#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        ListNode* p1 = l1, * p2 = l2;
        ListNode* head = new ListNode(-1);
        ListNode* cur = head;
        int carry = 0;
        while (p1 && p2)
        {
            int sum = p1->val + p2->val;
            int put = sum % 10 + carry;
            ListNode* newnode = new ListNode(put % 10);
            carry = (sum) / 10 + put / 10;
            cur->next = newnode;
            cur = cur->next;
            p1 = p1->next;
            p2 = p2->next;
        }
        while (p1)
        {
            int sum = p1->val;
            int put = sum + carry;
            ListNode* newnode = new ListNode(put % 10);
            carry = put / 10;
            cur->next = newnode;
            p1 = p1->next;
            cur = cur->next;
        }
        while (p2)
        {
            int sum = p2->val;
            int put = sum + carry;
            ListNode* newnode = new ListNode(put % 10);
            carry = put / 10;
            cur->next = newnode;
            p2 = p2->next;
            cur = cur->next;
        }
        while (carry)
        {
            ListNode* newnode = new ListNode(carry % 10);
            cur->next = newnode;
            cur = cur->next;
            carry /= 10;
        }
        return head->next;
    }
};